import React from 'react';
import { connect } from 'dva'
import { routerRedux } from 'dva/router'
import { Page } from 'components'
import { Card, Badge, Table, Divider } from 'antd';
import DescriptionList from 'components/DescriptionList';
import PageHeaderLayout from 'components/Layout/PageHeaderLayout';
import styles from './index.less';
import { financialSettlementStatus, paymentType, financialBusinessType, financialSettlementApplyHandleType, financialPaymentBillAuditStatus, financialPaymentBillPaymentStatus, financialBillHandleType } from "utils/enumConfig"
import moment from 'moment';

const FinancialPaymentBillDetails = ({ location, dispatch, financialPaymentBillDetail, loading }) => {

  const { Description } = DescriptionList;
  let { currentItemDetail } = financialPaymentBillDetail;

  let loaded = _.isEmpty(currentItemDetail);

  // 处理操作日志->按照创建时间排序
  const handleLogs = _.sortBy(currentItemDetail.handleLogs, (log) => {
    return -log.handleTime;
  });

  const breadcrumbList = [
    {
      title: '财务管理',
      href: '/financialPaymentBill'
    }, {
      title: '付款单',
      href: '/financialPaymentBill'
    }, {
      title: '查看详情'
    }
  ];

  const logsColumns = [
    {
      title: '操作人',
      dataIndex: 'handlerName',
      key: 'handlerName',
      render: (text, record, index) => (record.handlerName)
    }, {
      title: '操作时间',
      dataIndex: 'handleTime',
      key: 'handleTime',
      render: (text, record, index) => (moment(record.handleTime).format("YYYY-MM-DD HH:mm:ss"))
    }, {
      title: '操作类型',
      dataIndex: 'handleType',
      key: 'handleType',
      render: (text, record, index) => (financialBillHandleType[record.handleType])
    }
  ];

  const settlementApplyColumns = [
    {
      title: '结算单据编号',
      dataIndex: 'orderId',
      key: 'orderId',
      render: (text, record, index) => (record.orderId)
    }, {
      title: '应付金额',
      dataIndex: 'amountPayable',
      key: 'amountPayable',
      render: (text, record, index) => (record.settlementType === "PAYABLE"
        ? record.settlementMoney.toFixed(2)
        : "")
    },
    // {
    //   title: '应收金额',
    //   dataIndex: 'amountReceivable',
    //   key: 'amountReceivable',
    //   render: (text, record, index) => (record.settlementType === "RECEIVABLE"
    //     ? record.settlementMoney.toFixed(2)
    //     : "")
    // },
    {
      title: '业务类型',
      dataIndex: 'handleType',
      key: 'handleType',
      render: (text, record, index) => (financialBusinessType[record.businessType])
    }, {
      title: '结算时间',
      dataIndex: 'createTime',
      key: 'createTime',
      render: (text, record, index) => (moment(record.createTime).format("YYYY-MM-DD HH:mm:ss"))
    }
  ];

  return (
    <PageHeaderLayout title="付款单详情页" breadcrumbList={breadcrumbList}>
      {!loaded && <Card className={styles.card} bordered={false}>
        <DescriptionList
          size="large"
          title="付款单信息"
          style={{ marginBottom: 32 }}>
          <Description term="付款单号">{currentItemDetail.num}</Description>
          <Description term="应付金额">{currentItemDetail.payable.toFixed(2)}</Description>
          {!_.isEmpty(currentItemDetail.receiptName) && <Description term="收款方">{currentItemDetail.receiptName}</Description>}
          {/* <Description term="收款账户">{currentItemDetail.receiptAccount}</Description> */}
          <Description term="备注">{currentItemDetail.remark}</Description>
          <Description term="制单人">{currentItemDetail.creatorName}</Description>
          <Description term="支付方式">{currentItemDetail.paymentType === "NONE" ? "付款反充" : "线下付款"}</Description>
          <Description term="制单时间">{moment(currentItemDetail.createTime).format("YYYY-MM-DD HH:mm:ss")}</Description>
        </DescriptionList>
        <Divider style={{ marginBottom: 32 }} />
        {currentItemDetail.auditStatus !== "NULL" && (
          <div>
            <DescriptionList
              size="large"
              title="审核信息"
              style={{ marginBottom: 32 }}>
              <Description term="审核结果">{financialPaymentBillAuditStatus[currentItemDetail.auditStatus]}</Description>
              <Description term="审核人">{currentItemDetail.auditorName}</Description>
              <Description term="审核时间">{moment(currentItemDetail.auditTime).format("YYYY-MM-DD HH:mm:ss")}</Description>
              <Description term="备注">{currentItemDetail.auditRemark}</Description>
            </DescriptionList>
            <Divider style={{ marginBottom: 32 }} />
          </div>)}
        {currentItemDetail.paymentType !== "NONE" && (<div>
          <DescriptionList
            size="large"
            title="付款详情"
            style={{ marginBottom: 32 }}>
            <Description term="付款方式">{paymentType[currentItemDetail.paymentType]}</Description>
            <Description term="付款方">{currentItemDetail.payment}</Description>
            <Description term="付款账户">{currentItemDetail.paymentAccount}</Description>
            <Description term="支付凭证号">{currentItemDetail.paymentNum}</Description>
            <Description term="支付时间">{currentItemDetail.cashTime}</Description>
            <Description term="付款人">{moment(currentItemDetail.cashTime).format("YYYY-MM-DD HH:mm:ss")}</Description>
            <Description term="操作时间">{moment(currentItemDetail.cashHandleTime).format("YYYY-MM-DD HH:mm:ss")}</Description>
          </DescriptionList>
          <Divider style={{ marginBottom: 32 }} />
        </div>)}
        <div className={styles.title}>结算申请信息</div>
        <Table
          style={{
            marginBottom: 24
          }}
          pagination={false}
          dataSource={currentItemDetail.applySummaryList}
          columns={settlementApplyColumns}
          rowKey="id" />
        <div className={styles.title}>操作记录</div>
        <Table
          style={{
            marginBottom: 24
          }}
          pagination={false}
          dataSource={handleLogs}
          columns={logsColumns}
          rowKey="id" />
      </Card>}
    </PageHeaderLayout>
  )
};


export default connect(({ financialPaymentBillDetail, loading }) => ({ financialPaymentBillDetail, loading }))(FinancialPaymentBillDetails)
